package bamboo.drawingkid.controls;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;

import bamboo.drawingkid.Main;
import bamboo.drawingkid.views.ToolBarView;
import bamboo.drawingkid.views.components.Button;
import bamboo.drawingkid.views.components.ButtonListener;

public class ToolbarViewController extends ToolBarView implements ButtonListener, CommandListener {

	private final Main		midlet;
    private Alert 			alertQuit;
    private Alert			alertNew;

    private Command			cmdQuitAlertYes;
    private Command			cmdQuitAlertNo;
    private Command			cmdNewAlertYes;
    private Command			cmdNewAlertNo;
    private Command			cmdNewAlertCancel;

	public ToolbarViewController(int screenwidth, int screenheight) {
		super(screenwidth, screenheight);
		midlet = Main.getInstance();
		
    	registerControlsListener();
    	alertQuit = new Alert("Quit", "Do you want to save this document?", null, AlertType.CONFIRMATION);
    	alertNew = new Alert("Open a new one", "Do you want to save this document?", null, AlertType.WARNING);
    	
    	alertQuit.setTimeout(Alert.FOREVER);
    	alertQuit.addCommand(cmdQuitAlertYes = new Command("Yes", Command.OK, 1));
    	alertQuit.addCommand(cmdQuitAlertNo = new Command("No", Command.CANCEL, 2));
    	alertNew.setTimeout(Alert.FOREVER);
    	alertNew.addCommand(cmdNewAlertYes = new Command("Yes", Command.OK, 1));
    	alertNew.addCommand(cmdNewAlertNo = new Command("No", Command.CANCEL, 2));
    	alertNew.addCommand(cmdNewAlertCancel = new Command("Cancel", Command.CANCEL, 2));
    	
    	alertQuit.setCommandListener(this);
    	alertNew.setCommandListener(this);
	}

	/**
	 * 	Register listener for all controls in this toolbar
	 */
	private void registerControlsListener() {
    	btnQuit.addListener(this);
    	btnNew.addListener(this);
    	btnOpen.addListener(this);
    	btnSave.addListener(this);
    	btnColor.addListener(this);
    	btnBrushSize.addListener(this);
    	btnEraser.addListener(this);
    	btnTemplate.addListener(this);
    	btnSend.addListener(this);
	}

	public void render(Graphics g) {
		super.render(g);
	}

	/**
	 * Change all buttons in toolbar into normal or highlight state
	 * @param hightlight YES: highlight, otherwise normal
	 */
	private void changeButtonState(boolean hightlight) {
		for(int i = 0; i < tools.size(); i++){
			Button tool = (Button)tools.elementAt(i);
			tool.setHighlight(false);
		}
	}

	public void commandAction(Command c, Displayable d) {
		if(c == cmdQuitAlertYes) {
			Main.getInstance().notifyDestroyed();
		}else if (c == cmdQuitAlertNo) {
			Main.getInstance().showDrawing();
		}else if (c == cmdNewAlertYes) {
			//TODO: show a dialog to save the image
			Main.getInstance().showDrawing();
			DrawingViewController.getInstance().clearDrawing();
		}else if (c == cmdNewAlertNo) {
			Main.getInstance().showDrawing();
			DrawingViewController.getInstance().clearDrawing();
		}else if (c == cmdNewAlertCancel) {
			Main.getInstance().showDrawing();
		}
	}

	public void buttonPressed(Button sender) {
		changeButtonState(false);
		if(sender == btnQuit) {
			showAlertQuit();
		} else if(sender == btnNew) {
			showAlertNew();
		} else if(sender == btnOpen) {
			showOpenImageView();
		} else if(sender == btnSave) {
			showSaveProgress();
		} else if(sender == btnColor) {
			showColorsView();
		} else if(sender == btnBrushSize) {
			//TODO:
			showBrushSizeView();
		} else if(sender == btnEraser) {
			sender.setHighlight(true);
			DrawingViewController.getInstance().colorChanged(0xFFFFFFFF);
		} else if(sender == btnTemplate) {
			showTemplate();
		} else if(sender == btnSend) {
			showMmsEmailView();
		}
	}

	private void showOpenImageView() {
		DrawingViewController.getInstance().showOpenImageView();
	}

	private void showBrushSizeView() {
		// TODO: Change image of brush size button
		
	}

	private void showColorsView() {
		DrawingViewController.getInstance().showColorsView();
	}

	private void showSaveProgress() {
		DrawingViewController.getInstance().showSaveProgress();
	}

	private void showTemplate() {
		DrawingViewController.getInstance().showTemplate();
	}

	private void showMmsEmailView() {
		Main.getInstance().showMmsEmailView();
	}

	private void showAlertQuit() {
		Main.getInstance().getDisplay().setCurrent(alertQuit);
	}

	private void showAlertNew() {
		Main.getInstance().getDisplay().setCurrent(alertNew);
	}

}
